package com.interview.three;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;

import javax.annotation.PostConstruct;
import javax.inject.Provider;

/**
 * 学习构造方法注入的循环依赖问题
 * {@link TestSeterCircularDependency}
 */
@Slf4j
public class TestConstructorCircularDependency {


    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("a", A.class);
        context.registerBean("b", B.class);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        context.refresh();

        System.out.println(context.getBean(A.class).b.get());
        System.out.println(context.getBean(B.class));
    }
    @Slf4j
    static class A{
        private Provider<B> b;
        public A( Provider<B> b) {
            this.b =b;
            log.info("构造器注入-A(B b)：{}",b.getClass());
        }
  
        @PostConstruct
        public void init(){
            log.error("A初始化....");
        }
        public void fooA(){
            log.error("A类中的foo方法-------");
        }
    }

    @Slf4j
    static class B{
        private A a;

        public B(A a) {
            this.a =a;
            log.info("构造器注入- B(A a)：{}",a.getClass());
          
        }

  
        @PostConstruct
        public void init(){
            log.error("B初始化-----");
        }
    }
}
